//
//  HWRecommendDetailVc.m
//  JWC
//
//  Created by lanou on 16/4/16.
//  Copyright © 2016年 陈帅. All rights reserved.
//


#import "HWRecommendDetailVc.h"
#import "HWHeadTableViewCell.h"
#import "HWEssenceTool.h"
#import "MJRefresh.h"
#import "HWRecommentCell.h"
#import "HWDetailModel.h"
#import "HWCommentViewController.h"

static NSString *cellID = @"new";
@interface HWRecommendDetailVc ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)HWEssenceTool *tool;
@property (nonatomic, strong)HWDetailModel *detailModel;
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic, assign)NSInteger np;

@property (nonatomic, assign)BOOL isZheng;

@end

@implementation HWRecommendDetailVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.model.screen_name;
    
    [self setRefresh];
    [self getHeaderData];
    [self setupHeader];
    [self setTableView];
    [self enterComment];
    
    self.modelArray = [NSMutableArray array];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(more) image:@"navigationbar_more" highImage:@"navigationbar_more_highlighted"];
    _isZheng = YES;
}

- (void)more
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cencel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *save = [UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIAlertView *saveView = [[UIAlertView alloc]initWithTitle:nil message:@"感谢你的支持，此功能暂未开通!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [saveView show];
    }];
    UIAlertAction *report = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"提示" message:@"举报成功，感谢您的支持!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
    }];
    UIAlertAction *zheng = [UIAlertAction actionWithTitle:@"正序" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _isZheng = YES;
        [self.tableView reloadData];
    }];
    UIAlertAction *dao = [UIAlertAction actionWithTitle:@"倒序" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _isZheng = NO;
        [self.tableView reloadData];
    }];
    
    [alertController addAction:cencel];
    [alertController addAction:save];
    [alertController addAction:report];
    [alertController addAction:zheng];
    [alertController addAction:dao];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

#pragma mark --- 评论按钮 ---
- (void)enterComment
{
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"commentClick" object:nil] subscribeNext:^(NSNotification *noti) {
        HWFrame *topFrame = noti.userInfo[@"topFrame"];
        HWCommentViewController *commentVc =[[HWCommentViewController alloc]init];
        commentVc.frame = topFrame;
        [self.navigationController pushViewController:commentVc animated:YES];
    }];
}

#pragma mark --- 设置下拉刷新、上拉加载 ---
- (void)setRefresh
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
    
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
}

- (void)getNewData
{
    [self.tool getDetailDataWithID:self.model.uid block:^(id json, NSInteger np) {
        for (HWRecommendModel *model in json)
        {
            HWFrame *frame = [[HWFrame alloc]init];
            frame.model = model;
            [self.modelArray addObject:frame];
        }
        self.np = np;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
    [self.tableView.mj_header endRefreshing];
}

- (void)getMoreData
{
    [self.tool getDetailDataWithID:self.model.uid np:self.np block:^(id json, NSInteger np) {
        for (HWRecommendModel *model in json)
        {
            HWFrame *frame = [[HWFrame alloc]init];
            frame.model = model;
            [self.modelArray addObject:frame];
        }
        self.np = np;
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)getHeaderData
{
    
}

#pragma mark --- 设置表头 ---
- (void)setupHeader
{
    
    [self.tool getHeadWithID:self.model.uid block:^(id json) {
        self.detailModel = json;
        [self.tableView reloadData];
        
        UIView *header = [[UIView alloc]init];
        
        HWHeadTableViewCell *cell = [HWHeadTableViewCell cell];
        cell.model = self.detailModel;
        cell.size = CGSizeMake(SCREEN_WIDTH, 320);
        
        [header addSubview:cell];
        header.frame = CGRectMake(0, 0, SCREEN_WIDTH, 320);
        self.tableView.tableHeaderView = header;
    }];
}

#pragma mark --- 设置tableView ---
- (void)setTableView
{
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.tableView registerNib:[UINib nibWithNibName:@"HWRecommentCell" bundle:nil] forCellReuseIdentifier:cellID];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isZheng)
    {
        HWRecommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        cell.topFrame = self.modelArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    else
    {
        HWRecommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        cell.topFrame = self.modelArray[self.modelArray.count - indexPath.row - 1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"帖子";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isZheng)
    {
        HWFrame *frame = self.modelArray[indexPath.row];
        return frame.cellHeight;
    }
    else
    {
        HWFrame *frame = self.modelArray[self.modelArray.count - indexPath.row - 1];
        return frame.cellHeight;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isZheng)
    {
        HWFrame *frame = self.modelArray[indexPath.row];
        HWCommentViewController *commentVc = [[HWCommentViewController alloc]init];
        commentVc.frame = frame;
        [self.navigationController pushViewController:commentVc animated:YES];
    }
    else
    {
        HWFrame *frame = self.modelArray[self.modelArray.count - indexPath.row - 1];
        HWCommentViewController *commentVc = [[HWCommentViewController alloc]init];
        commentVc.frame = frame;
        [self.navigationController pushViewController:commentVc animated:YES];
    }
    
}

// 懒加载
- (HWEssenceTool *)tool{
    if (_tool == nil) {
        _tool = [[HWEssenceTool alloc]init];
        
    }
    return _tool;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
